/**
 * Copyright © 2009 Qasky. All rights reserved.
 *
 * 整个项目的状态管理
 *
 * @author Wang Hongya
 * @version 0.0.0 2019/10/14
 *
 * History:
 * (author, date, desc)
 * (Wang Hongya, 20191014, 添加注释)
 */
const app = {
    state: {
        cachePage: [],
        pageOpenedList: [{
            title: '首页',
            path: '',
            name: 'centerSetting'
        }],
      openedSubmenuArr: [], // 要展开的菜单数组
      currentPageName: '',
        currentPath: [
            {
                title: '首页',
                path: '',
                name: 'centerSetting'
            }
        ], // 面包屑数组
        menuList: [],
        routers: [],
        rootSubmenuKeys: [],
    },
    mutations: {
        /*******************************************************************************
         * Function    : updateMenulist
         * Description : 更新按钮状态
         * Input       : state,状态
         * Return      : None
         *******************************************************************************/
        updateMenulist (state) {
            state.routers = JSON.parse(sessionStorage.getItem("menuList"));
            let menuList = [];
            let rootSubmenuKeys = [];
            state.routers.forEach((item, index) => {
                if (item.children.length === 1) {
                    menuList.push(item);
                } else {
                    const len = menuList.push(item);
                    let childrenArr = item.children;
                    if (childrenArr === undefined || childrenArr.length === 0) {
                        menuList.splice(len - 1, 1);
                    } else {
                        const handledItem = JSON.parse(JSON.stringify(menuList[len - 1]));
                        handledItem.children = childrenArr;
                        menuList.splice(len - 1, 1, handledItem);
                    }
                }
            });

            /**
             * 初始化挂载首页的处理
             */
            menuList.splice(0, 1);
            state.menuList = menuList;
            menuList.forEach(item=>{
                rootSubmenuKeys.push(item.key)
            })
            state.rootSubmenuKeys = rootSubmenuKeys
        },

        /*******************************************************************************
         * Function    : addOpenSubmenu
         * Description : 添加打开的子菜单
         * Input       : state, 状态
         *               name, 名称
         * Return      : None
         *******************************************************************************/
        addOpenSubmenu (state, name) {
            let hasThisName = false;
            let isEmpty = false;
            if (name.length === 0) {
                isEmpty = true;
            }
            if (state.openedSubmenuArr.indexOf(name) > -1) {
                hasThisName = true;
            }
            if (!hasThisName && !isEmpty) {
                state.openedSubmenuArr.push(name);
            }
        },

        /*******************************************************************************
         * Function    : pageOpenedList
         * Description : 打开列表
         * Input       : state, 状态
         *               get,获取状态
         * Return      : None
         *******************************************************************************/
        pageOpenedList (state, get) {
            let openedPage = state.pageOpenedList[get.index];
            if (get.argu) {
                openedPage.argu = get.argu;
            }
            if (get.query) {
                openedPage.query = get.query;
            }
            state.pageOpenedList.splice(get.index, 1, openedPage);
            localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        },


        /*******************************************************************************
         * Function    : setCurrentPath
         * Description : 设置当前路径
         * Input       : state, 状态
         *               pathArr, 当前路径
         * Return      : None
         *******************************************************************************/
        setCurrentPath (state, pathArr) {
            state.currentPath = pathArr;
        },

        /*******************************************************************************
         * Function    : setCurrentPageName
         * Description : 设置当前界面名称
         * Input       : state, 状态
         *               name, 界面名称
         * Return      : None
         *******************************************************************************/
        setCurrentPageName (state, name) {
            state.currentPageName = name;
        },


        /*******************************************************************************
         * Function    : clearOpenedSubmenu
         * Description : 关闭打开的子菜单
         * Input       : state, 状态
         * Return      : None
         *******************************************************************************/
        clearOpenedSubmenu (state) {
            state.openedSubmenuArr.length = 0;
        },


    }
};

export default app;
